package util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Random;

/**
 * User: tum0r
 * Package Name: util
 * Time: 2019-03-10 Sunday 15:52:18
 */
public class VerificationCode {
    public String getVerificationCodeImageBase64(String verificationCode) throws IOException {
        BufferedImage out = getVerificationCodeBufferedImage(verificationCode);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(out, "png", byteArrayOutputStream);
        byte[] result = byteArrayOutputStream.toByteArray();
        return "data:image/png;base64," + Base64.getEncoder().encodeToString(result);
    }

    public byte[] getVerificationCodeByteArray(String verificationCode) throws IOException {
        BufferedImage out = getVerificationCodeBufferedImage(verificationCode);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(out, "png", byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }

    public BufferedImage getVerificationCodeBufferedImage(String verificationCode) {
//        Random random = new Random();
        BufferedImage out = new BufferedImage(700, 300, BufferedImage.TYPE_INT_ARGB);
        out = setPoint(out);
        Graphics2D graphics2D = (Graphics2D) out.getGraphics();
        graphics2D.setFont(new Font("", Font.PLAIN, 180));
        char[] code = verificationCode.toCharArray();
        int x = 60;
        for (int i = 0; i < code.length; i++) {
//            graphics2D.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
            graphics2D.setColor(new Color(0,0,0));
//            double rotate = random.nextInt(15) * Math.PI / 360;
//            if (random.nextBoolean()) {
//                rotate = -rotate;
//            }
//            graphics2D.rotate(rotate, x+50, 250);
            graphics2D.drawChars(code, i, 1, x, 200);
            x += 100;
        }
        return out;
    }

    public BufferedImage setPoint(BufferedImage input) {
        int block = 25;
        int width = input.getWidth();
        int height = input.getHeight();
        Random random = new Random();
        for (int x = 0; x < width; x += block) {
            for (int y = 0; y < height; y += block) {
                if (random.nextInt(10) < 4) {
                    int r = random.nextInt(256);
                    int g = random.nextInt(256);
                    int b = random.nextInt(256);
                    int x_random = x + random.nextInt(block);
                    int y_random = y + random.nextInt(block);
                    for (int i = x; i < x_random; i++) {
                        for (int j = y; j < y_random; j++) {
                            input.setRGB(i, j, new Color(r, g, b, 100).getRGB());
                        }
                    }
                }
            }
        }
        return input;
    }
}
